<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome_admin extends CI_Controller {
	
	function __construct(){
			parent :: __construct();
			$this->check_isvalidated();
	
	}
	
	public function index($page = 'admin_home') //displays login page on admin side
	{
		if ( ! file_exists('application/views/'.$page.'.php'))
		{
			// Whoops, we don't have a page for that!
			show_404();
		}
	
		$data['title'] = ucfirst($page); // Capitalize the first letter
		$this->load->helper('url');
		//$this->load->view('templates/header_admin', $data);
		/*if ($page == 'admin_login')
		{
			$this->load->view($page, $data);
			$this->load->view('templates/footer', $data);
		}
		else
		{*/
			$this->load->view('templates/header_admin', $data);
			$this->load->view($page, $data);
			$this->load->view('templates/footer', $data);
		//}
	}
	
	private function check_isvalidated(){
		
		$this->load->helper('url');
		if(! $this->session->userdata('validated'))
		{
			//$msg = 'please-login-to-continue';
			redirect('login/not_loggedin');
		}
	}
	
	public function do_logout(){
		$this->load->helper('url');
		$this->session->sess_destroy();
		redirect('login');
	}
	
	public function admin_report_write()
	{
		$this->load->helper('url');
		$this->load->helper('ckeditor');
		
		//Ckeditor's configuration
		$data['ckeditor'] = array(
		//ID of the textarea that will be replaced
				'id' 	=> 	'text',
				'path'	=>	'js/ckeditor',
				//Optionnal values
				'config' => array(
						'toolbar' 	=> 	"Full", 	//Using the Full toolbar
						'width' 	=> 	"550px",	//Setting a custom width
						'height' 	=> 	'100px',	//Setting a custom height
				),
		
				//Replacing styles from the "Styles tool"
				'styles' => array(
						//Creating a new style named "style 1"
						'style 1' => array (
								'name' 		=> 	'Blue Title',
								'element' 	=> 	'h2',
								'styles' => array(
										'color' 	=> 	'Blue',
										'font-weight' 	=> 	'bold'
								)
						),
						//Creating a new style named "style 2"
						'style 2' => array (
								'name' 	=> 	'Red Title',
								'element' 	=> 	'h2',
								'styles' => array(
										'color' 		=> 	'Red',
										'font-weight' 		=> 	'bold',
										'text-decoration'	=> 	'underline'
								)
						)
				)
		);
		$this->load->model('news_model');
		$this->load->helper('form');
		$this->load->library('form_validation');
	
		$data['title'] = 'Write an article';
		$article_type = 'news';
		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('text', 'text', 'required');
	
		if ($this->form_validation->run() === FALSE)
		{
			$this->load->view('templates/header_admin', $data);
			$this->load->view('admin_write_report', $data);
			$this->load->view('templates/footer');
	
		}
		else
		{
			$this->news_model->set_news();
			$this->load->view('templates/header_admin', $data);
			$this->load->view('success');
			$this->load->view('templates/footer');
		}
	}
	
	public function admin_report_index() //Displays the view where all the incidents repored by students
										//	are displayed
										// Need to add option to delete, modify and add comments to reports.
	{
	
		$this->load->helper('url');
		$this->load->model('news_model');
		$data['reports'] = $this->news_model->get_news('reports');
		$data['title'] = 'Reports archive';
	
		$this->load->view('templates/header_admin', $data);
		$this->load->view('admin_report_index', $data);
		$this->load->view('templates/footer');
	}
	
	public function admin_homework()
	{
		$this->load->helper('url');
		$data['title'] = '';
		$this->load->view('templates/header_admin', $data);
		$this->load->view('admin_homework_index', $data);
		$this->load->view('templates/footer', $data);
	}
	
	public function admin_news_index() //displays index of news articles already posted
	{
		$this->load->helper('url');
		$this->load->model('news_model');
		$data['news'] = $this->news_model->get_news();
		$data['mode'] = 'admin';
		$data['type'] = 'news';
		$data['title'] = 'News archive';
		$this->load->view('templates/header_admin', $data);
		$this->load->view('news_index', $data);
		$this->load->view('templates/footer');
	}
	
	public function admin_news_view($slug) //displays separately, news articles already posted
	{
		$this->load->helper('url');
		$this->load->model('news_model');
		$data['news_item'] = $this->news_model->get_news('news', $slug);
		$data['mode'] = 'admin';
		$data['type'] = 'news';
		if (empty($data['news_item']))
		{
			show_404();
		}
	
		$data['title'] = $data['news_item']['title'];
		$this->load->view('templates/header_admin', $data);
		$this->load->view('news', $data);
		$this->load->view('templates/footer');
	}
	
	public function admin_gossip_index() //displays index of gossip articles already posted
	{
		$this->load->helper('url');
		$this->load->model('news_model');
		$data['news'] = $this->news_model->get_news('gossip');
		$data['title'] = 'Gossips posted';
		$data['mode'] = 'admin';
		$data['type'] = 'gossip';
		$this->load->view('templates/header_admin', $data);
		$this->load->view('news_index', $data);
		$this->load->view('templates/footer', $data);
	}
	
	public function admin_gossip_view($slug) //displays separately, news articles already posted
	{
		$this->load->helper('url');
		$this->load->model('news_model');
		$data['news_item'] = $this->news_model->get_news('gossip', $slug);
		$data['mode'] = 'admin';
		$data['type'] = 'gossip';
		if (empty($data['news_item']))
		{
			show_404();
			
		}
		else
		{
			$data['title'] = $data['news_item']['title'];
			$this->load->view('templates/header_admin', $data);
			$this->load->view('news', $data);
			$this->load->view('templates/footer');
		}
	}
	
	public function admin_password_change()
	{
		$this->load->helper('url');
		$this->load->model('news_model');
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$data['title'] = 'Change password';
		
		$this->form_validation->set_rules('old_password', 'text', 'required');
		$this->form_validation->set_rules('new_password', 'text', 'required');
		$this->form_validation->set_rules('confirm_password', 'text', 'required');
		
		if ($this->form_validation->run() === FALSE)
		{
			$this->load->view('templates/header_admin', $data);
			$this->load->view('admin_password_change', $data);
			$this->load->view('templates/footer');
		
		}
		
		else 
		{
			if ($this->news_model->change_password())
			{	
				//$this->news_model->change_password();
				$this->load->view('templates/header_admin', $data);
				$this->load->view('success');
				$this->load->view('templates/footer');
			}			
		
			else
			{
				$this->load->view('templates/header_admin', $data);
				$this->load->view('admin_password_change', $data);
				$this->load->view('templates/footer');
			}
		}
	}

}
